package cn.iocoder.yudao.module.promotion.combination.vo;

import cn.iocoder.yudao.framework.validation.InEnumValue;
import cn.iocoder.yudao.module.api.product.sku.dto.ProductPropertyValueRespDTO;
import cn.iocoder.yudao.module.api.promotion.combination.enums.CombinationRecordStatusEnum;
import cn.iocoder.yudao.module.api.shop.info.dto.ShopInfoDTO;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.annotation.Nonnull;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.module.api.product.ConfigConstants.PROPERTY_VALUE_NAME_DEFAULT;

@Schema(description = "用户 App - 拼团记录 Response VO")
@Data
public class AppCombinationRecordRespVO {

    @Schema(description = "拼团记录编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
    private Long id;

    @Schema(description = "店铺编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
    private Long shopId;

    @Schema(description = "店铺信息")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private ShopInfoDTO shop;

    @Schema(description = "拼团活动编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
    private Long activityId;

    @Schema(description = "用户昵称", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
    private String nickname;

    @Schema(description = "用户头像", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
    private String avatar;

    @Schema(description = "过期时间", requiredMode = Schema.RequiredMode.REQUIRED)
    private LocalDateTime expireTime;

    @Schema(description = "可参团人数", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")
    private Integer userSize;

    @Schema(description = "已参团人数", requiredMode = Schema.RequiredMode.REQUIRED, example = "5")
    private Integer userCount;

    @Schema(description = "拼团状态：0-进行中、1-拼团成功、2-拼团失败", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    @InEnumValue(value = CombinationRecordStatusEnum.class)
    private Integer status;

    @Schema(description = "订单编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "2048")
    private Long orderId;

    @Schema(description = "产品 SPU 编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "2048")
    private Long spuId;

    @Schema(description = "产品名字", requiredMode = Schema.RequiredMode.REQUIRED, example = "我是大黄豆")
    private String spuName;

    @Schema(description = "产品图片", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://exmaple.local/1.png")
    private String picUrl;

    @Schema(description = "产品 SKU 编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "2048")
    private Long skuId;

    @Schema(description = "产品属性")
    private List<ProductPropertyValueRespDTO> properties;

    @Nonnull
    public List<ProductPropertyValueRespDTO> getProperties() {
        if (properties == null) {
            properties = new ArrayList<>();
        }
        return properties;
    }

    @Schema(description = "产品属性")
    private String propertiesValue;

    @SuppressWarnings("unused")
    public String getPropertiesValue() {
        if (propertiesValue != null) {
            return propertiesValue;
        }
        return getProperties().stream()
                .map(ProductPropertyValueRespDTO::getValueName)
                .filter(it -> !PROPERTY_VALUE_NAME_DEFAULT.equals(it))
                .reduce((a, b) -> a + "+" + b)
                .orElse("");
    }

    @Schema(description = "购买的产品数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
    private Integer count;

    @Schema(description = "拼团金额，单位：分", requiredMode = Schema.RequiredMode.REQUIRED, example = "100")
    private Integer combinationPrice;

}
